package com.capgemini.excel.util;

import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

public class ExcelExportUtil {

	public static SXSSFWorkbook generateExcel(List<List<String>> content, List<String> header) {
		SXSSFWorkbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory,
													// exceeding rows will be
													// flushed to disk
		Sheet sh = wb.createSheet();
		Row hRow = sh.createRow(0); // header row
		int columnIndex = 0;
		for (; columnIndex < header.size(); columnIndex++) {
			Cell hCell = hRow.createCell(columnIndex);
			hCell.setCellValue(header.get(columnIndex));
		}

		for (int rowIndex = 0; rowIndex < content.size(); rowIndex++) {
			Row row = sh.createRow(rowIndex+1);	
			List<String> contentRow = content.get(rowIndex);
			for(columnIndex = 0; columnIndex < contentRow.size(); columnIndex++) {
				Cell ccell = row.createCell(columnIndex);
//				System.out.println(contentRow.get(columnIndex) + " , " + rowIndex + " , " + columnIndex);
				ccell.setCellValue(contentRow.get(columnIndex));
			}
		}

//		FileOutputStream out;
//		try {
//			out = new FileOutputStream("D:/tmp/sxssf.xlsx");
//			wb.write(out);
//			out.close();
//			// dispose of temporary files backing this workbook on disk
//			wb.dispose();
//			wb.close();
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}

		return wb;
	}
}
